package com.zhs.gccs.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hjq.shape.layout.ShapeFrameLayout;
import com.hjq.shape.view.ShapeImageView;
import com.zhs.gccs.BuildConfig;
import com.zhs.gccs.R;
import com.zhs.gccs.model.GccsTaskFile;
import com.zhs.gccs.utils.SharedPrefHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chengzf on 2024/7/22.
 */
public class GccsImageAdapter extends BaseQuickAdapter<GccsTaskFile, BaseViewHolder> {
    static int TYPE_DEFAULT = 101;
    static int TYPE_IMAGE = 102;
    private int selectedPosition = 0;
    ArrayList<GccsTaskFile> list = new ArrayList<>();

    public GccsImageAdapter() {
        super(new ArrayList<>());
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

//    public GccsImageAdapter() {
//        super(R.layout.item_image_view);
//    }

    public void setComplaintData(ArrayList<GccsTaskFile> data) {
        // 查看的时候不能添加
        list.clear();
        GccsTaskFile gccsTaskFile = new GccsTaskFile();
        gccsTaskFile.setDefault(true);
        list.add(gccsTaskFile);
        list.addAll(data);
        super.setNewData(list);
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_DEFAULT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_bar_code_check_default_view_detail_add, parent, false);
            return new NormalViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_image_view, parent, false);
            return new ContractViewHolder(view);
        }
    }

    class ContractViewHolder extends BaseViewHolder {

        public ContractViewHolder(View view) {
            super(view);
        }

    }

    class NormalViewHolder extends BaseViewHolder {

        public NormalViewHolder(View view) {
            super(view);
        }

    }


    @Override
    protected int getDefItemViewType(int position) {
        if (mData.get(position).isDefault()) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_IMAGE;
        }
    }
    @Override
    protected void convert(@NonNull BaseViewHolder holder, GccsTaskFile bean) {
//        ShapeImageView image = holder.getView(R.id.iv_image);
//        ShapeFrameLayout frame_icon = holder.getView(R.id.frame_icon);
//        if(TextUtils.isEmpty(gccsTaskFile.getLocalUrl())){
//            String path = BuildConfig.BASEURL+"gccsFile/preview/image/"+gccsTaskFile.getId()
//                    +"?token="+ SharedPrefHelper.getInstance().getToken();
//            Glide.with(mContext). load(path).into(image);
//        }else {
//            Glide.with(mContext). load(gccsTaskFile.getLocalUrl()).into(image);
//        }
//        if (holder.getBindingAdapterPosition() == selectedPosition){
//            frame_icon.getShapeDrawableBuilder().setStrokeColor(Color.parseColor("#FFFFFF")).intoBackground();
//        }else {
//            frame_icon.getShapeDrawableBuilder().setStrokeColor(Color.parseColor("#00000000")).intoBackground();
//        }
        if (holder instanceof ContractViewHolder) {
            ShapeImageView cover = holder.getView(R.id.iv_image);
            ShapeFrameLayout frame_icon = holder.getView(R.id.frame_icon);
            if (TextUtils.isEmpty(bean.getLocalUrl())) {
                String path = BuildConfig.BASEURL + "gccsFile/preview/image/" + bean.getId()
                        + "?token=" + SharedPrefHelper.getInstance().getToken();
                Glide.with(mContext).load(path).into(cover);
            } else {
                Glide.with(mContext).load(bean.getLocalUrl()).into(cover);
            }
            if (holder.getBindingAdapterPosition() == selectedPosition){
                frame_icon.getShapeDrawableBuilder().setStrokeColor(Color.parseColor("#FFFFFF")).intoBackground();
            }else {
                frame_icon.getShapeDrawableBuilder().setStrokeColor(Color.parseColor("#00000000")).intoBackground();
            }
        } else {

        }
    }
}
